<template>
  <div class="login-container">
    <div class="login-layout">
      <!-- 左侧区域 - 使用SVG插图 -->
      <div class="left-panel">
        <div class="illustration-container">
          <img src="@/assets/学习.svg" alt="学习插图" class="illustration" />
        </div>
      </div>
      
      <!-- 右侧区域 -->
      <div class="right-panel">
        <div class="form-container">
          <div class="login-header">
            <div class="logo-wrapper">
              <img src="@/assets/logo.png" alt="学习平台" class="logo-image" />
              <span class="text-learn">学无止境</span>
            </div>
            <p class="subtitle">LEARNING RECOMMENDATION SYSTEM</p>
          </div>
          
          <el-form :model="loginForm" :rules="rules" ref="loginFormRef" class="login-form">
            <el-form-item prop="username">
              <el-input 
                v-model="loginForm.username"
                :prefix-icon="User"
                placeholder="请输入用户名">
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input 
                v-model="loginForm.password"
                :prefix-icon="Lock"
                type="password"
                placeholder="请输入密码">
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :loading="loading" @click="handleLogin" class="login-button">
                登录
              </el-button>
            </el-form-item>
            <div class="auth-links">
              <router-link to="/register">立即注册</router-link>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/store/user'
import request from '@/utils/request'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = () => {
  loginFormRef.value.validate(async valid => {
    if (valid) {
      loading.value = true
      try {
        await request.post("/user/login", loginForm, {
          successMsg: "登录成功",
          showDefaultMsg: true,
          onSuccess: (data) => {
            userStore.setUserInfo(data)
            
            // 根据角色重定向到相应页面
            if (data.role === 'ADMIN') {
              // 管理员重定向到后台
              router.push(route.query.redirect || '/back/dashboard')
            } else {
              // 普通用户重定向到前台首页
              router.push(route.query.redirect || '/')
            }
          }
        })
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: url('@/assets/bg.jpeg') center center / cover no-repeat;
  padding: 0;
}

.login-layout {
  display: flex;
  width: 80%;
  max-width: 1200px;
  min-height: 600px;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid rgba(235, 238, 245, 0.2);
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(5px);
  background-color: rgba(255, 255, 255, 0.8);
}

.left-panel {
  flex: 1;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  position: relative;
}

.illustration-container {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.illustration {
  max-width: 90%;
  max-height: 90%;
  object-fit: contain;
}

.right-panel {
  flex: 1;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-container {
  width: 80%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
  
  .logo-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 10px;
    
    .logo-image {
      height: 40px;
      width: auto;
      margin-right: 10px;
    }
    
    .text-learn {
      color: #4776E6;
      font-size: 25px;
      font-weight: 800;
      letter-spacing: 1px;
    }
  }
  
  .subtitle {
    font-size: 14px;
    color: #909399;
    letter-spacing: 1px;
  }
}

.login-form {
  :deep(.el-input) {
    --el-input-bg-color: #ffffff;
    --el-input-border-color: #dcdfe6;
    --el-input-hover-border-color: #c0c4cc;
    --el-input-focus-border-color: var(--el-color-primary);
    
    .el-input__wrapper {
      box-shadow: none !important;
      border: 1px solid #dcdfe6;
      border-radius: 4px;
    }

    input {
      color: #606266;
      height: 40px;

      &::placeholder {
        color: #c0c4cc;
      }
    }
  }
}

.login-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
  border-radius: 4px;
  margin-top: 10px;
  background: linear-gradient(to right, #409eff, #53a8ff);
  border: none;
  
  &:hover {
    background: linear-gradient(to right, #66b1ff, #79bbff);
  }
  
  &:active {
    background: linear-gradient(to right, #3a8ee6, #4ca0ff);
  }
}

.auth-links {
  text-align: center;
  margin-top: 20px;
  
  a {
    color: #409eff;
    text-decoration: none;
    font-weight: 500;
    
    &:hover {
      color: #66b1ff;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .login-layout {
    flex-direction: column;
    width: 95%;
    border: none;
  }
  
  .left-panel {
    padding: 20px;
    min-height: 250px;
  }
  
  .form-container {
    padding: 30px 0;
  }
}
</style> 